package zasm.zasm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class LineReader implements ILineReader {

	private final BufferedReader	in;
	private int						lineNumber	= 0;
	private String					nextLine;

	public LineReader(final Reader in, final int sz) throws IOException {
		this.in = new BufferedReader(in, sz);
		readFirstLine();
	}

	public LineReader(final Reader in) throws IOException {
		this.in = new BufferedReader(in);
		readFirstLine();
	}

	public LineReader(final File file) throws FileNotFoundException, IOException {
		this(new FileReader(file));
	}

	public LineReader(final String fileName) throws FileNotFoundException, IOException {
		this(new FileReader(fileName));
	}

	private void readFirstLine() throws IOException {
		nextLine = in.readLine();
	}

	@Override
	public String peekLine() {
		return nextLine;
	}

	@Override
	public String readLine() throws IOException {
		final String line = nextLine;
		nextLine = in.readLine();
		lineNumber++;
		return line;
	}

	@Override
	public int getLineNumber() {
		return lineNumber;
	}

	@Override
	public int getNextLineNumber() {
		return lineNumber + 1;
	}

	@Override
	public void close() throws IOException {
		in.close();
	}

}
